/*
 * Copyright (c) 2006-2020, Chengdu RST technology co., ltd
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-12-14     shijiabao       the first version
 */
#ifndef APPLICATIONS_COMMON_HEADER_COMMON_HEADER_H_
#define APPLICATIONS_COMMON_HEADER_H_
#define P_EN 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dfs_posix.h>
#include <dfs.h>
#include <at.h>
#include <rtdbg.h>

#include <arpa/inet.h>
#include <netdev.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/types.h>
#include <unistd.h>
#include <netdb.h>

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <drv_spi.h>
#include "spi_flash.h"
#include "spi_flash_sfud.h"
#include <CHIP_FLASH/bsp_flash.h>
#include "drv_flash.h"
#include <PRIVATE_PROTOCOL/rst_private_protocol_api.h>
#include <CONFIGURE/device_configure.h>
#include <SPI_FLASH/bsp_spi_flash.h>
#include <PRIVATE_PROTOCOL/private_protocol.h>
#include <UART/bsp_uart.h>
#include <UART/uart_recv_thread.h>
#include <LED/bsp_led.h>
#include <TASK/task_thread.h>
#include <GPIO/bsp_gpio.h>
#include <LTE/lte_init.h>
#include <LOG/ulog_file_api.h>
#include <FATFS/fat_ulog_api.h>
#include <dtu_rtc/dtu_rtc.h>
#include <WATCH_DOG/watch_dog.h>
#include <DATA_CACHE/data_cache.h>
#include <SOCKET_API/socket_api.h>
#include <time.h>
#include <rtdef.h>
#include <at_device_sim76xx.h>
#include <SOCKET_API/socket_api.h>
extern char port0[2];
extern char port1[2];
extern char port2[2];

//用于接收socket数据的缓存空间
extern char sock_recv_data_buff[1048];

extern struct lte_data_struct
{
    /*模块初始化状态*/
    uint8_t lte_init_flag;
    /*模块网络连接状态*/
    uint8_t lte_net_flag;
    /*模块错误提示*/
    uint8_t lte_err_flag;
    //APN连接标志位
    uint8_t apn_connect_flag;
    //通道服务器连接标志
    uint8_t channel_connect_flag[3];
}lte_data;

//DTU参数结构体，包括DTU配置和实时状态
static struct device_configure
{
    //device parm
    char product_name[0x0A];
    char hardware_version[0x06];
    char soft_version[0x06];
    char serial_number[0x14];
    char dtu_number[0x0c];
    //apn parm
    char apn_using_flag[0x04];
    char apn_name[0x20];
    char apn_user[0x20];
    char apn_password[0x20];
    //data transmission parm
    char data_transmission_mode[0x02];
    char register_packet[0x20];
    char network_mode[0x10];
    char breathe_packet[0x20];
    char breathe_time[0x04];
    //data cneter
    char data_center1_ip[0x20];
    char data_center1_port[0x06];
    char data_center2_ip[0x20];
    char data_center2_port[0x06];
    char private_data_center_ip[0x20];
    char private_data_center_port[0x06];
    //data center using setting
    char data_center1_using_flag[0x04];
    char data_center2_using_flag[0x04];
    //serial parm
    char rs485_baud_rate[0x0A];
    char rs485_data_bit[0x02];
    char rs485_stop_bit[0x02];
    char rs485_check_bit[0x07];
    char rs232_baud_rate[0x0A];
    char rs232_data_bit[0x02];
    char rs232_stop_bit[0x02];
    char rs232_check_bit[0x07];
    //private instruction
    char private_instruction1[0x20];
    char private_instruction2[0x20];
    char private_instruction3[0x20];
    char private_instruction4[0x20];
    //mqtt parm
    char mqtt_using_flag[0x04];
    char mqtt_url[0x20];
    char mqtt_username[0x20];
    char mqtt_password[0x20];
    char mqtt_subtopic[0x20];
    char mqtt_pubtopic[0x20];
    char mqtt_willmsg[0x20];
};

static struct device_status
{
    char device_ip[0x20];
    char device_imei[0x20];
    //设备温度
    int device_temp;
    //设备时间
    int device_time;
    uint8_t network_init_flag;
    uint8_t network_connect_status;
    u_int8_t network_error_flag;
    u_int8_t apn_connect_flag;
    int transparent_transmission_server1_connect_flag;
    int transparent_transmission_server2_connect_flag;
    int private_server_connect_flag;
    uint8_t mqtt_server_connect_flag;
    int transparent_transmission1_sockfd;
    int transparent_transmission2_sockfd;
    int private_transmission_sockfd;
};

extern struct device_parm
{
    struct device_configure dn400_configure;
    struct device_status dn400_status;
    /*datacenter1*/
    struct rt_tcpclient_t *tcpclient1;
    /*datacenter2*/
    struct rt_tcpclient_t *tcpclient2;
    /*private datacenter*/
    struct rt_tcpclient_t *tcpclient3;
}dn400_parm;

rt_device_t debug_serial;

//通道接收数据结构定义
extern struct lte_rx_msg
{
    //通道号
    uint8_t channel;
}rx_msg;

struct rt_messagequeue lte_recv_data_mq;
/* 用于接收消息的信号量 */
struct rt_semaphore rx_sem;

/*定义时间结构体的指针*/
struct tm* now_t;
time_t now;

/*DTU时间结构体*/
extern struct  dtu_time
{
  int  year;
  int  month;
  int  day;
  int  hour;
  int  minute;
  int  second;
}dtu_times;

#endif /* APPLICATIONS_COMMON_HEADER_COMMON_HEADER_H_ */
